package cn.jasonone.jfx;

import javafx.application.Preloader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.effect.Reflection;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

/**
 * @author xujie
 * @version 1.0
 * @date 2021/12/22 11:58
 */
public class JfxPreloader extends Preloader {
    public static final String BANNER = "jfx.banner";
    private ProgressBar progressBar;
    private Label logo;
    private Label status;
    private Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        progressBar = new ProgressBar(0);
        progressBar.setPrefHeight(25);
        progressBar.setProgress(-1);

        Reflection reflection = new Reflection();
        reflection.setBottomOpacity(0.0);
        reflection.setTopOpacity(0.5);
        reflection.setTopOffset(0.0);
        reflection.setFraction(0.7);
        logo = new Label();
        logo.setAlignment(Pos.CENTER);

        Text text = new Text(System.getProperty(BANNER, "JFX FRAMEWORK"));
        text.setEffect(reflection);
        text.setFont(Font.font("宋体", 64));
        text.setFill(Color.DARKSEAGREEN);
        logo.setGraphic(text);
        logo.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));

        status = new Label("状态");
        status.setPrefHeight(25);
        status.setAlignment(Pos.CENTER);
        status.setTextFill(Color.BLUE);
        AnchorPane root = new AnchorPane(progressBar, logo, status);
        AnchorPane.setBottomAnchor(progressBar, 0D);
        AnchorPane.setRightAnchor(progressBar, 0D);
        AnchorPane.setLeftAnchor(progressBar, 0D);
        AnchorPane.setBottomAnchor(status, 0D);
        AnchorPane.setRightAnchor(status, 0D);
        AnchorPane.setLeftAnchor(status, 0D);
        AnchorPane.setBottomAnchor(logo, 25D);
        AnchorPane.setTopAnchor(logo, 0D);
        AnchorPane.setLeftAnchor(logo, 0D);
        AnchorPane.setRightAnchor(logo, 0D);
        Scene scene = new Scene(root, 800, 600);
        scene.setFill(Color.WHITE);
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.show();
        stage = primaryStage;
    }

    @Override
    public void handleProgressNotification(ProgressNotification info) {
        progressBar.setProgress(info.getProgress());
    }

    public void handleLabelNotification(LabelNotification info) {
        status.setText(info.getLabel());
        if (info.color != null) {
            status.setTextFill(info.color);
        }
    }

    @Override
    public void handleStateChangeNotification(StateChangeNotification info) {
        if (info.getType() == StateChangeNotification.Type.BEFORE_START) {
            stage.close();
        }
    }

    @Override
    public void handleApplicationNotification(PreloaderNotification info) {
        if (info instanceof LabelNotification) {
            handleLabelNotification((LabelNotification) info);
        } else if (info instanceof ProgressNotification) {
            handleProgressNotification((ProgressNotification) info);
        } else if (info instanceof StateChangeNotification) {
            handleStateChangeNotification((StateChangeNotification) info);
        }
    }

    @Data
    @AllArgsConstructor
    @RequiredArgsConstructor
    public static class LabelNotification implements PreloaderNotification {
        private final String label;
        private Paint color = Color.BLUE;
    }
}
